/* $RCSFile: ADNDynAnnotation.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynAnnotation.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:48  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Data;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynAnnotation : ADNDynCommonDoc
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.annotation)
            {}

            public override Type DynType
            { get { return typeof(ADNDynAnnotation); } }

            public override string DynTypename
            {   get{ return CrmStr.annotation;}}

            public override DynEntity Build()
            {   return new ADNDynAnnotation();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynAnnotation(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynAnnotation(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynAnnotation(w,PrimaryKeyValue);}
        }

        static ADNDynAnnotation(){new Ftor();} 

        public override bool HasOwnerProperty
        {
            get
            {
                return true;
            }
        }

        public override bool HasOwningBusinessUnitProperty
        {
            get
            {
                return true;
            }
        }

        public override string SchemaName
        {
           get
           {
               if ( string.IsNullOrEmpty(_SchemaName) )
                    _SchemaName =   CrmStr.annotation;
               return _SchemaName;
           }
           set{}
        }


        public override ADNTypeCodes ADNTypeCode
        {   get{ return ADNTypeCodes.Annotation ; }}


        public override int ObjectTypeCode
        {
           get
           {
               if ( _ObjectTypeCode == 0 )
                    _ObjectTypeCode    =   5;
               return _ObjectTypeCode;
           }
           set{}
        }
        
        public ADNDynAnnotation(): base()
		{}

        public ADNDynAnnotation(CRMWrapperBase w, string PrimaryFiedlValue)
			: base(w, CrmStr.annotation)
		{
            if ( PrimaryFiedlValue != string.Empty )
                subject    =   PrimaryFiedlValue;
		}

		public ADNDynAnnotation(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.annotation, CrmId)
		{}

		public ADNDynAnnotation(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.annotation,e,null,null)
		{}

		public ADNDynAnnotation(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.annotation,e,prop,s)
		{}

        public ADNDynAnnotation(ResetWrapperHandler fn, string PrimaryFiedlValue)
			: base(fn, CrmStr.annotation)
		{
            if ( PrimaryFiedlValue != string.Empty )
                subject    =   PrimaryFiedlValue;
		}

		public ADNDynAnnotation(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.annotation, CrmId)
		{}

		public ADNDynAnnotation(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.annotation,e,null,null)
		{}

		public ADNDynAnnotation(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.annotation,e,prop,s)
		{}

		/// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public Guid annotationid
		{
			get
			{
				return KeyPropertyValue(CrmStr.annotationid);
			}
			set
			{
				AddKeyProperty(CrmStr.annotationid, value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.annotationid) || KeyPropertyValue(CrmStr.annotationid) != crmId )
                annotationid   =   crmId;
        }
        
        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.subject;
            }
           set
           {}
        }

        public override string PrimaryFieldStringValue
        {
            get
            {
                return subject;
            }
            set
            {
                subject =   value;
            }
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return annotationid;
           }
           set
           {
               annotationid    =   value;
           }
        }

        /// <summary>
        /// Gets or sets the subject of the annotation. 
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public string subject 
        {
            get
            {
                return StringPropertyValue(CrmStr.subject);
            }
            set
            {
                AddStringProperty(CrmStr.subject,value);
            }
        }

        /// <summary>
        /// Gets or sets the mimetype of the annotation attached file. 
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public string notetext 
        {
            get
            {
                return StringPropertyValue(CrmStr.notetext);
            }
            set
            {
                AddStringProperty(CrmStr.notetext,value);
            }
        }
        
        /// <summary>
        /// Contains the description of the subject. 
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public string documentbody 
        {
            get
            {
                return StringPropertyValue(CrmStr.documentbody);
            }
            set
            {
                AddStringProperty(CrmStr.documentbody,value);
            }
        }

        /// <summary>
        /// Contains the description of the subject. 
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public string langid 
        {
            get
            {
                return StringPropertyValue(CrmStr.langid);
            }
            set
            {
                AddStringProperty(CrmStr.langid,value);
            }
        }

        /// <summary>
        /// Must be set before setting objectid, default: account
        /// </summary>
        string _DefaultObjectName  =   CrmStr.account;
        public string DefaultObjectName
        {
            get
            {
                return _DefaultObjectName;
            }
            set
            {
                _DefaultObjectName  =   value;
            }
        }

        /// <summary>
        /// Gets or sets the ID of the subject associated with the knowledge base article 
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public Guid objectid
        {
            get
            {
                if (ContainsKey(CrmStr.objectid))
                    return LookupPropertyValue(CrmStr.objectid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.objectid,DefaultObjectName,value);
            }
        }

        [CrmDynAttribute(CrmStr.annotation)]
        public string objectName
        {
            get
            {
                if (ContainsKey(CrmStr.objectid))
                    return LookupPropertyName(CrmStr.objectid);
                return string.Empty;
            }
        }
        
        /// <summary>
        /// EntityNameReference
        /// Must set DefaultObjecttypecodeName before setting objecttypecode
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public string objecttypecode 
        {
            get
            {
                return EntityNameReferencePropertyValue(CrmStr.objecttypecode);
            }
            set
            {
                AddEntityNameReferenceProperty(CrmStr.objecttypecode,value);
            }
        }

        [CrmDynAttribute(CrmStr.annotation)]
        public Guid owningbusinessunit
        {
            get
            {
                return LookupPropertyValue(CrmStr.owningbusinessunit);
            }
            set
            {
                AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);
            }
        }

        public string owningbusinessunitName
        {
            get
            {
                return LookupPropertyName(CrmStr.owningbusinessunit);
            }
        }

        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.annotation)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }
        #region Utilities
           
        public override string MakeOpenUrlFmt()
        {
            int objectTpCode    =   (int)CRMUtil.typeCode(CrmStr.salesliteratureitem);

            string url          =   string.Format("{0}/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId={{{{{{0}}}}}}",
                                                                    wrapper.DownloadServerUrl);
            if (CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynAnnotation MakeOpenUrlFmt salesliteratureitem Url: {0}.", url), CRMDynTraceSwitch.Sw.Info);
            return url;
        }
        

        static string[] ExAnnotationAttributes              =   new string[]{CrmStr.annotationid,
                                                                CrmStr.subject,
                                                                CrmStr.stepid,
                                                                CrmStr.notetext,
                                                                CrmStr.langid,
                                                                CrmStr.mimetype,
                                                                CrmStr.isdocument,
                                                                CrmStr.filename,
                                                                CrmStr.filesize,
                                                                CrmStr.ownerid,
                                                                CrmStr.objecttypecode,
                                                                CrmStr.objectid,
                                                                CrmStr.modifiedby,
                                                                CrmStr.modifiedon,
                                                                CrmStr.createdby,
                                                                CrmStr.createdon,
                                                                CrmStr.importsequencenumber,
                                                                CrmStr.owningbusinessunit
                                                            };

        static string[] AnnotationAttributes                =   new string[]{CrmStr.annotationid,
                                                                CrmStr.subject,
                                                                CrmStr.notetext,
                                                                CrmStr.mimetype,
                                                                CrmStr.filename,
                                                                CrmStr.filesize,
                                                                CrmStr.ownerid,
                                                                CrmStr.objecttypecode,
                                                                CrmStr.objectid
                                                                };

        /// <summary>
        /// Build a DataTable to display in in RadGrid
        /// </summary>
        public static DataTable ReadForGrid(CRMWrapperBase wrapper,ResetWrapperHandler fn,bool extendedView)
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynAnnotation ReadForGrid BEG extendedView: {0}.",extendedView),(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            DataTable Tb                                        =   null;
            try
            {
                InternalQueryExpression qe                      =   new InternalQueryExpression();
                qe.EntityName                                   =   CrmStr.annotation;
                InternalColumnSet RequestCols                   =   new InternalColumnSet();
                InternalFilterExpression fi                     =   new InternalFilterExpression();
                InternalConditionExpression ex                  =   new InternalConditionExpression();
                if ( extendedView )
                        RequestCols.Attributes                  =   ExAnnotationAttributes;
                else    RequestCols.Attributes                  =   AnnotationAttributes;

                ex.AttributeName                                =   CrmStr.isdocument;
                ex.Operator                                     =    InternalConditionOperator.Equal;
                ex.Values                                       =   new object[]{true};
                fi.Conditions                                   =   new InternalConditionExpression[]{ex};
                qe.ColumnSet                                    =   RequestCols;
                qe.Criteria                                     =   fi;
                GenericDynQueryMultiple<ADNDynAnnotation> allEntQuery      =   new GenericDynQueryMultiple<ADNDynAnnotation>(wrapper,fn,CrmStr.annotation);
                Tb                                              =   allEntQuery.retrieveDatatable(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynAnnotation ReadForGrid ex  {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynAnnotation ReadForGrid END Count: {0}",Tb.Rows.Count),(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            return Tb;
        }

        /// <summary>
        /// Build a SortedList 
        /// </summary>
        static public SortedList<Guid,ADNDynAnnotation>  ReadAll(CRMWrapperBase wrapper,ResetWrapperHandler fn)
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]ADNDynAnnotation ReadAll BEG.",(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            SortedList<Guid,ADNDynAnnotation> Lst      =   null;
            try
            {
                InternalQueryExpression qe                      =   new InternalQueryExpression();
                qe.EntityName                                   =   CrmStr.annotation;
                InternalFilterExpression fi                     =   new InternalFilterExpression();
                InternalConditionExpression ex                  =   new InternalConditionExpression();
                ex.AttributeName                                =   CrmStr.isdocument;
                ex.Operator                                     =    InternalConditionOperator.Equal;
                ex.Values                                       =   new object[]{true};
                fi.Conditions                                   =   new InternalConditionExpression[]{ex};
                qe.ColumnSet                                    =   new InternalAllColumns();
                qe.Criteria                                     =   fi;
                GenericDynQueryMultiple<ADNDynAnnotation> allEntQuery      =   new GenericDynQueryMultiple<ADNDynAnnotation>(wrapper,fn,CrmStr.annotation);
                Lst                                             =   allEntQuery.retrieveSortedList(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynAnnotation ReadAll ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynAnnotation ReadAll END Count: {0}",Lst.Count),(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            return Lst;
        }

        #endregion
     }
}
